package cn.david.basic.controller;

import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping
    public AjaxResult upload(@RequestPart (required = true,value = "file")MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".") + 1);
            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.me().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上传失败");
        }

    }
    @DeleteMapping
    public AjaxResult delete(@RequestParam (required = true, value = "path")String path){
        try {
            String pathTmp = path.substring(1);
            String groupName = pathTmp.substring(0, pathTmp.indexOf("/"));
            String romotePath = pathTmp.substring(pathTmp.lastIndexOf("/") + 1);
            FastDfsUtil.delete(groupName, romotePath);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

}
